package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.UserDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/10/23 15:57
 * @Version V1.0
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    // 用户
    @Autowired
    UserDao userDao;

    @Override
    public User findUserByUsername(String username) {
        return userDao.findUserByUsername(username);
    }

    @Override
    public void updatePassword(Map map) {
        //1.拿出用户名和密码去数据库查询，看看是否有该用户：
        String username=(String)map.get("username");
        String oldPassword=(String)map.get("oldPassword");
        String newPassword=(String)map.get("newPassword");
        //
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        //根据用户名去数据库茶密码：
       String dbOldPassword =userDao.findPasswordByUsername(username);
        boolean matches = bCryptPasswordEncoder.matches(oldPassword,dbOldPassword);
        if(matches){
            //如果用户输入的旧密码和数据库中的旧密码相同就可以改密码：
            String newDbPassword = bCryptPasswordEncoder.encode(newPassword);
            userDao.updatePasswordByUsername(newDbPassword,username);
        }else {
           throw  new RuntimeException("您的密码修改失败,因为您输入的密码有误");
        }
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = userDao.pageQuery(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /*添加 编辑 第二项的数据*/
    @Override
    public List<Role> findRoleList() {
        return userDao.findRoleList();
    }

    /*添加操作*/
    @Override
    public void add(User user, Integer[] roleIds) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String pasword = passwordEncoder.encode("123");
        user.setPassword(pasword);
        userDao.add(user);
        setUserAndRole(user.getId(), roleIds);
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public List<Integer> findroleIds(Integer id) {
        return userDao.findroleIds(id);
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        userDao.edit(user);
        userDao.deleteUserIdByRoleIds(user.getId());
        setUserAndRole(user.getId(), roleIds);
    }

    @Override
    public void delete(Integer id) {
        long Total = userDao.findRoleCountByUserId(id);
        if (Total>0){
            throw new RuntimeException("当前用户和角色之间存在关联关系，不能删除");
        }
        userDao.delete(id);

    }

    private void setUserAndRole(Integer userId, Integer[] RoleIds) {
        if (RoleIds != null && RoleIds.length > 0) {
            for (Integer permissionId : RoleIds) {
                // 方案一：在CheckGroupDao中@Param
                // checkGroupDao.addCheckGroupAndCheckItem(roleId,checkItemId);
                // 方案二：使用Map集合
                Map<String, Object> map = new HashMap<>();
                map.put("User_Id", userId);
                map.put("Role_Id", permissionId);
                userDao.addUserAndRole(map);
            }
        }
    }
}
